var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all) __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if ((from && typeof from === 'object') || typeof from === 'function') {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, {
          get: () => from[key],
          enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
        });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (
  (target = mod != null ? __create(__getProtoOf(mod)) : {}),
  __copyProps(
    // If the importer is in node compatibility mode or this is not an ESM
    // file that has been converted to a CommonJS file using a Babel-
    // compatible transform (i.e. "__esModule" has not been set), then set
    // "default" to the CommonJS "module.exports" for node compatibility.
    isNodeMode || !mod || !mod.__esModule
      ? __defProp(target, 'default', { value: mod, enumerable: true })
      : target,
    mod,
  )
);
var __toCommonJS = (mod) => __copyProps(__defProp({}, '__esModule', { value: true }), mod);

// src/Ellipsis/index.tsx
var Ellipsis_exports = {};
__export(Ellipsis_exports, {
  default: () => Ellipsis,
});
module.exports = __toCommonJS(Ellipsis_exports);
var import_react = __toESM(require('react'));
var import_classnames = __toESM(require('classnames'));
var import_index = require('./index.less');
var px2Num = (px) => {
  return typeof px === 'number' ? px : Number(px.replace('px', ''));
};
function Ellipsis({
  className,
  style,
  content,
  expandText = '展开',
  collapseText = '收起',
  defaultExpanded,
  rows = 1,
}) {
  const [expandable, setExpandable] = (0, import_react.useState)(true);
  const [expanded, setExpanded] = (0, import_react.useState)(!!defaultExpanded);
  const [ellipsisText, setEllipsisText] = (0, import_react.useState)(content);
  const containerRef = (0, import_react.useRef)(null);
  const refresh = (0, import_react.useCallback)(() => {
    if (containerRef.current) {
      const style2 = window.getComputedStyle(containerRef.current);
      const width = px2Num(style2.width);
      const height = px2Num(style2.height);
      const vnode = document.createElement('div');
      vnode.style.setProperty('font-family', style2.fontFamily);
      vnode.style.setProperty('font-weight', style2.fontWeight);
      vnode.style.setProperty('font-size', style2.fontSize);
      vnode.style.setProperty('letter-spacing', style2.letterSpacing);
      vnode.style.setProperty('line-height', style2.lineHeight);
      vnode.style.setProperty('width', 'max-content');
      vnode.style.setProperty('word-break', 'break-all');
      vnode.style.setProperty('text-wrap', 'no-wrap');
      vnode.textContent = content;
      document.body.appendChild(vnode);
      const vnodeStyle = window.getComputedStyle(vnode);
      const vnodeWidth = px2Num(vnodeStyle.width);
      const vnodeHeight = px2Num(vnodeStyle.height);
      if (height <= vnodeHeight * rows || !content.length) {
        setExpandable(false);
      }
      const singleRowLength = content.length * (width / vnodeWidth);
      const rowsLength = singleRowLength * rows;
      const adjustOffset = 3;
      let expandedLength = rowsLength - (expandText.length + adjustOffset);
      if (expandedLength < 0 || isNaN(expandedLength)) expandedLength = 0;
      expandedLength = Math.round(expandedLength);
      vnode.style.width = width + 'px';
      let text = '';
      let currentHeight = 0;
      let prevExpandedLength = 0;
      const setExpandedTextToVnode = (currentExpandedLength) => {
        prevExpandedLength = expandedLength;
        text = content.slice(0, currentExpandedLength) + '...';
        vnode.textContent = `${text} ${expandText}`;
        currentHeight = px2Num(window.getComputedStyle(vnode).height);
      };
      setExpandedTextToVnode(expandedLength);
      if (currentHeight <= rows * vnodeHeight) {
        while (currentHeight <= rows * vnodeHeight && expandedLength < content.length - 1) {
          let nextExpandedLength = expandedLength + 1;
          setExpandedTextToVnode(nextExpandedLength);
          if (currentHeight <= rows * vnodeHeight) {
            expandedLength = nextExpandedLength;
          } else {
            text = content.slice(0, prevExpandedLength) + '...';
          }
        }
      } else {
        while (currentHeight > rows * vnodeHeight && expandedLength < content.length - 1) {
          let nextExpandedLength = expandedLength - 1;
          setExpandedTextToVnode(nextExpandedLength);
          if (currentHeight > rows * vnodeHeight) {
            expandedLength = nextExpandedLength;
          }
        }
      }
      setEllipsisText(text);
      document.body.removeChild(vnode);
    }
  }, [content, defaultExpanded, expandText]);
  (0, import_react.useLayoutEffect)(() => {
    refresh();
  }, []);
  return /* @__PURE__ */ import_react.default.createElement(
    'div',
    {
      className: (0, import_classnames.default)('szzj-ellipsis', className),
      style,
      ref: containerRef,
    },
    !expandable || expanded ? content : ellipsisText,
    expandable &&
      /* @__PURE__ */ import_react.default.createElement(
        'a',
        {
          className: 'szzj-ellipsis-expand',
          onClick: () => setExpanded(!expanded),
        },
        ' ',
        expanded ? collapseText : expandText,
      ),
  );
}
